sink("log/coding_log.txt", append = FALSE, type = "output")

## Packages
library(tidyverse)

## Read data
data <- read.csv("data/IPW_review.csv")
data$Year[data$Year == 999] <- 2022
data$Year <- factor(data$Year, levels = 2004:2022)

## Figure A.1 (Online Supplementary Materials A)
subset(data, IPW == 1) %>% 
	count(Year, name = "IPW", .drop = FALSE) %>%
	right_join(subset(data, EB == 1) %>% count(Year, name = "EB", .drop = FALSE), by = "Year") %>%
	right_join(subset(data, PSM == 1) %>% count(Year, name = "PSM", .drop = FALSE), by = "Year") %>%
	right_join(subset(data, GM == 1) %>% count(Year, name = "GM", .drop = FALSE), by = "Year") %>%
	pivot_longer(data = ., cols = !Year, names_to = "Method", values_to = "count") %>%
	mutate(Year = as.integer(as.character(Year))) %>%
	filter(Year >= 2008) %>%
ggplot(., aes(x = Year, y = count, group = Method, color = Method)) +
	geom_line(size = 2, alpha = 0.65) +
	scale_color_viridis_d(end = 0.9, labels = c(IPW = "Inverse probability weighting", 
																							EB = "Entropy balancing",
																							PSM = "PS matching",
																							GM = "Genetic matching")) +
  geom_hline(yintercept = 0, linetype = "dotted", color = "grey40", size = 0.5) +
  xlab("") +
  ylab("Number of published articles") +
  scale_x_continuous(breaks = c(2010, 2015, 2020)) +
  scale_y_continuous(breaks = c(0, 5, 10)) +
  theme_classic() +
  theme(legend.position = c(0.18, 0.8),
        axis.title = element_text(size = 10),
        axis.title.x = element_text(size = 0),
        axis.text = element_text(size = 10),
        plot.title = element_text(hjust = 0.5, size = 12),
        legend.title = element_text(size = 0),
        legend.text = element_text(size = 9.5),
        legend.background = element_rect(fill = NA, colour = NA))
ggsave("figures/fig_a1.pdf", width = 17, height = 6, units = "cm")
#ggsave("figures/method.pdf", width = 17, height = 6, units = "cm")

sink()
